package com.github.prontera.controller;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.common.filter.IgnoreAuth;
import com.github.prontera.domain.Activity;
import com.github.prontera.domain.Client;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.ActivityService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 活动
 */
@RestController
@Api(value = "活动")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/activity")
public class ActivityController{

	@Autowired
	private ActivityService activityService;
	
	/**
	 * 查询用户活动列表
	 */
	@IgnoreAuth
	
	@RequestMapping(value = "get", method = {RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "单条获取活动详情" , notes = "单条获取活动详情")
	public ObjectDataResponse<Activity> get(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Activity findActivity){
		if(findActivity.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"activityId必传!");
		}
		findActivity.setClientId(clientId);
		Activity activity=activityService.findOne(findActivity);
		if(activity!=null){
			activity.setContent(null);
		}
		activity.setDiffEndTime(activity.getEndTime().toEpochSecond()-OffsetDateTime.now().toEpochSecond());
		return new ObjectDataResponse<Activity>(activity);
	}

	/**
	 * 参与活动用户列表
	 */
	
	@RequestMapping(value = "findActivityClients", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "参与活动用户列表" , notes = "参与活动用户列表")
	public ObjectDataResponse<List<Client>> findActivityClients(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Activity findActivity){
		if(findActivity.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"activityId必传!");
		}
		findActivity.setClientId(clientId);
		List<Client> datas=activityService.findActivityClients(findActivity);
		return new ObjectDataResponse<List<Client>>(datas);
	}
	
	/**
	 * 查询用户活动列表
	 */
	@IgnoreAuth
	
	@RequestMapping(value = "findClientActivity", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户活动列表" , notes = "用户活动列表")
	public ObjectDataResponse<List<Activity>> findClientActivity(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId){
		List<Activity> datas=activityService.findClientActivity(clientId);
		for (Activity activity : datas) {
			activity.setContent(null);
			activity.setDiffEndTime(activity.getEndTime().toEpochSecond()-OffsetDateTime.now().toEpochSecond());
		}
		return new ObjectDataResponse<List<Activity>>(datas);
	}
	
	/**
	 * 查询活动列表
	 */
	@IgnoreAuth
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查询活动列表" , notes = "查询活动列表")
	public ObjectDataResponse<List<Activity>> find(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Activity activity){
		if(activity.getStatus()!=null&&activity.getStatus().intValue()==1){//
			activity.setHasCurrentActivity(true);
			activity.setStatus(null);
			List<Integer> list=new ArrayList<Integer>();
			list.add(0);
			list.add(1);
			list.add(2);
			activity.setStatusList(list);
		}else{//往期活动
			activity.setHasCurrentActivity(false);
			activity.setStatus(null);
			List<Integer> list=new ArrayList<Integer>();
			list.add(3);
			list.add(4);
			activity.setStatusList(list);
		}
		activity.setClientId(clientId);
		activity.setOrderBy("A_ACTIVITY.create_time");
		List<Activity> datas=activityService.find2(activity);
		return new ObjectDataResponse<List<Activity>>(datas);
	}
	
}
